package cn.iocoder.yudao.module.shop.controller.admin.shopOpenTime;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.shopOpenTime.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopOpenTime.ShopOpenTimeDO;
import cn.iocoder.yudao.module.shop.service.shopOpenTime.ShopOpenTimeService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 商户营业时间")
@RestController
@RequestMapping("/dc/shop-open-time")
@Validated
public class ShopOpenTimeController {

    @Resource
    private ShopOpenTimeService shopOpenTimeService;

    @PostMapping("/create")
    @Operation(summary = "创建商户营业时间")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:create')")
    public CommonResult<Long> createShopOpenTime(@Valid @RequestBody ShopOpenTimeSaveReqVO createReqVO) {
        return success(shopOpenTimeService.createShopOpenTime(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户营业时间")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:update')")
    public CommonResult<Boolean> updateShopOpenTime(@Valid @RequestBody ShopOpenTimeSaveReqVO updateReqVO) {
        shopOpenTimeService.updateShopOpenTime(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateBatch")
    @Operation(summary = "更新商户营业时间")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:update')")
    public CommonResult<Boolean> updateShopOpenTimeBatch(@Valid @RequestBody List<ShopOpenTimeSaveReqVO> list) {
        shopOpenTimeService.updateShopOpenTimeBatch(list);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户营业时间")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:delete')")
    public CommonResult<Boolean> deleteShopOpenTime(@RequestParam("id") Long id) {
        shopOpenTimeService.deleteShopOpenTime(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户营业时间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:query')")
    public CommonResult<ShopOpenTimeRespVO> getShopOpenTime(@RequestParam("id") Long id) {
        ShopOpenTimeDO shopOpenTime = shopOpenTimeService.getShopOpenTime(id);
        return success(BeanUtils.toBean(shopOpenTime, ShopOpenTimeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户营业时间分页")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:query')")
    public CommonResult<PageResult<ShopOpenTimeRespVO>> getShopOpenTimePage(@Valid ShopOpenTimePageReqVO pageReqVO) {
        PageResult<ShopOpenTimeDO> pageResult = shopOpenTimeService.getShopOpenTimePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopOpenTimeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商户营业时间列表")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:query')")
    public CommonResult<List<ShopOpenTimeRespVO>> getShopOpenTimeList(@Valid ShopOpenTimePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopOpenTimeDO> list = shopOpenTimeService.getShopOpenTimePage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, ShopOpenTimeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户营业时间 Excel")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShopOpenTimeExcel(@Valid ShopOpenTimePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopOpenTimeDO> list = shopOpenTimeService.getShopOpenTimePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户营业时间.xls", "数据", ShopOpenTimeRespVO.class,
                        BeanUtils.toBean(list, ShopOpenTimeRespVO.class));
    }

}